package com.opengroupe.service.user.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;

import com.opengroupe.dao.CandidatReponseQuestionDao;
import com.opengroupe.entities.Candidat;
import com.opengroupe.entities.CandidatReponseQuestion;
import com.opengroupe.entities.CandidatReponseQuestionId;
import com.opengroupe.entities.Question;
import com.opengroupe.entities.Reponse;
import com.opengroupe.service.user.UserService;

public class UserServiceImpl implements UserService {

	@Autowired
	private CandidatReponseQuestionDao candidatReponseQuestionDao;

	private static final Log logger = LogFactory.getLog(UserServiceImpl.class);

	public void repondreQuestion(Candidat candidat, Question question,
			List<Reponse> reponsesEventuelles) throws DataAccessException {

		for (Reponse reponseEventuelle : reponsesEventuelles) {
			
			CandidatReponseQuestion candidatReponseQuestion = new CandidatReponseQuestion();

			candidatReponseQuestion.setCandidat(candidat);
			candidatReponseQuestion.setQuestion(question);
			candidatReponseQuestion.setReponse(reponseEventuelle);
			candidatReponseQuestion.setId(new CandidatReponseQuestionId(
					candidat.getIdCandidat(), question.getIdQuestion(),
					reponseEventuelle.getIdReponse()));

			try {
				candidatReponseQuestionDao.persist(candidatReponseQuestion);
			} catch (DataAccessException e) {
				logger.error("Erreur lors de la sauvegarde de la réponse du candidat");
				throw e;
			}
		}
	}
}
